package com.aistar.service.impl;

import com.aistar.mapper.CompanyMapper;
import com.aistar.mapper.JobMapper;
import com.aistar.pojo.*;
import com.aistar.service.JobService;
import com.aistar.util.MyBatisUtil;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.fabric.Server;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JobServiceImpl implements JobService {


    @Override  // 查询最新的10 条记录
    public ServerResponse getSomeLatest() {
      SqlSession session =  MyBatisUtil.getSqlSession();
        JobMapper mapper = session.getMapper(JobMapper.class);
        JobExample example = new JobExample();
        example.createCriteria().andStatusEqualTo(0);
        example.setOrderByClause("job_publish_time desc");

        Page<Job> page = PageHelper.startPage(1,10);
        List<Job> jobList =  mapper.selectByExample(example);// select
        MyBatisUtil.closeSqlSession();


        /*select * from job j
        where status = 0
        order by j.job_publish_time desc
        limit 0,10*/
        return ServerResponse.getDataSuccess(jobList);
    }


    @Override
    public ServerResponse getByJobNameAndKeyword(String kw,Integer pageNum) {
      /*  SqlSession session =  MyBatisUtil.getSqlSession();
        JobMapper mapper = session.getMapper(JobMapper.class);
        JobExample example = new JobExample();
        if(kw!=null){
            JobExample.Criteria criteria1 = example.createCriteria();
            JobExample.Criteria criteria2 = example.createCriteria();

            criteria1.andJobNameLike(kw).andStatusEqualTo(0);
            criteria2.andJobSearchKeywordLike(kw).andStatusEqualTo(0);
            example.or(criteria1);
            example.or(criteria2);
        }else{
            example.createCriteria().andStatusEqualTo(0);
        }
        example.setOrderByClause("job_publish_time desc");


        Page<Job> page = PageHelper.startPage(pageNum,5);
        List<Job> jobList = mapper.selectByExample(example);
        PageInfo<Job> pageInfo = page.toPageInfo();

        System.out.println(jobList.size());
        for(Job job:jobList){
            System.out.println(job.getJobId()+"," + job.getJobName()+ ","+job.getJobSearchKeyword()+","+ job.getJobPublishTime());
        }




        if(jobList!=null && jobList.size()>0)
            return ServerResponse.getDataSuccess(pageInfo)  ;
        else
            return  ServerResponse.getDataFailed("对不起，没有找到符合你条件的职位！")    ;

*/
      return null;
    }
    @Override
    public ServerResponse getAll(String kw, String city,Date publishTime, Float minSalary, Float maxSalary,
                                  String workYear, String degree, String company, Integer pageNum) {

        List<JobCompanyVO> jobCompanyVOList = new ArrayList<>();

        SqlSession session =  MyBatisUtil.getSqlSession();
        JobMapper mapper = session.getMapper(JobMapper.class);
        Page<Job> page = PageHelper.startPage(pageNum,5);
        List<Job> jobList= mapper.selectAllJobBySearch(kw,city,publishTime,minSalary,maxSalary,workYear,degree,company);
        PageInfo pageInfo = page.toPageInfo();

        for(Job job:jobList){
            System.out.println(job.getJobId() + "," + job.getJobName() +","+ job.getJobMinSalary() + ","+job.getJobMaxSalary()
            + ","+ job.getJobPublishTime());
        }
        // 再封装Company , 根据job_id 查询所属公司 company
        CompanyMapper companyMapper = session.getMapper(CompanyMapper.class);

        // select * from company where company_id = ? (company_id 来源于 Job对象中的)
        for(Job job:jobList){
            Company com = companyMapper.selectByPrimaryKey(job.getCompanyId());
            jobCompanyVOList.add(new JobCompanyVO(job,com));
        }


        pageInfo.setList(jobCompanyVOList);  //重新封装PageInfo 中的数据为 jobCompanyVOList

MyBatisUtil.closeSqlSession();
        if(jobCompanyVOList!=null && jobCompanyVOList.size()>0)
            return  ServerResponse.getDataSuccess(pageInfo);
        return ServerResponse.getDataFailed("对不起，没有找到与您匹配的岗位");
    }


    @Override
    public ServerResponse getAllJob(String kw, Date publishTime, Float minSalary, Float maxSalary, String workYear, String degree, String company, Integer pageNum) {
        return null;
    }

    @Override
    public ServerResponse save(Job job) {
        return null;
    }

    @Override
    public ServerResponse modifyStatus(Job job) {
        return null;
    }

    @Override
    public ServerResponse modify(Job job) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {
        SqlSession session = MyBatisUtil.getSqlSession();
        JobMapper mapper = session.getMapper(JobMapper.class);
        Job job = mapper.selectByPrimaryKey(id);

        if(job==null) {
            MyBatisUtil.closeSqlSession();
            return ServerResponse.getDataFailed("暂无岗位");

        }  else{
            CompanyMapper companyMapper= session.getMapper(CompanyMapper.class);
            Company company = companyMapper.selectByPrimaryKey(job.getCompanyId());
            JobCompanyVO jobCompanyVO = new JobCompanyVO(job,company);
            MyBatisUtil.closeSqlSession();
            return ServerResponse.getDataSuccess(jobCompanyVO);
        }

    }

    @Override
    public ServerResponse getAll() {
        return null;
    }
}
